<?php
/**
 * Created by PhpStorm.
 * User: 吴登科
 * Date: 2018/10/31
 * Time: 14:42
 * 菜品详情
 */

namespace Home\Controller;

use Think\Controller;
use think\Session;

class DishesController extends Controller
{
    public $customer_id = '';
    public $store_id = 0;

    /*
     * 初始化
     */
    public function _initialize()
    {
        if (session('?customer_id') && session('?STORE_ID')) {
            $this->customer_id = session('customer_id');//用户id
            $this->store_id = session('STORE_ID');//店铺id
        }
        $this->customer_id = "20181102115031877722";
        $this->store_id = '11';
    }

    /**
     * 菜品详情页
     */
    public function details()
    {
        $dishes_id = I('get.dishes_id');//菜品id

        //获取菜品详情信息
        if ($dishes_id) {
            $count_data = array();
            //获取详细菜品信息
            $dishes_data = M('dishes')
                ->alias('d')
                ->field('d.*,dc.name as category')
                ->join('left join meal_dishes_category as dc on d.category_id=dc.id')
                ->where(['d.store_id' => $this->store_id, 'd.id' => $dishes_id])
                ->find();

            //获取菜品属性信息
            $dishes_attributes_info=M('dish_attributes')->field('id,attribute_value_name,attribute_value_price')->where(['dish_id'=>$dishes_id])->select();
//dump($dishes_attributes_info);die;

            //月销
            $begin_time = strtotime(date("Y-m-01 00:00:00"));
//           echo $begin;die;
            $map['o.pay_time'] = array(array('gt', $begin_time), array('lt', time()));
            $map['o.order_state'] = 1;
            $map['od.dish_id'] = $dishes_id;
            $map['o.store_id'] = $this->store_id;

            $sale_count = M('order_detail')
                ->alias('od')
                ->join('left join meal_dishes_order as o on od.order_no=o.order_no')
                ->field('od.count')
                ->where($map)
                ->select();

            $month_sale = 0;
            foreach ($sale_count as $item) {
                $month_sale += (int)$item['count'];
            }
//            echo '<pre>';
//            var_dump($sum);

            //好评率
            //当前商品评论总数
            $where['_string'] = 'FIND_IN_SET(' . "'$dishes_id'" . ',dishes_ids)';
            $where['store_id'] = $this->store_id;
            $total = M('comment')->where($where)->count();
//            echo '总评论数：'.$total;
            //当前商品好评总数
            $haoping = M('comment')->where($where)->where(['comment_level' => ['gt', 3]])->count();
//              echo '好评：'.$haoping;
            $haoping_rate = round(($haoping / $total) * 100);
//            echo $haoping_rate;

            //折扣
            $discount = round(($dishes_data['price'] / $dishes_data['original_price']) * 100);

            //菜品平均评论等级
            $avg_comment_level = M('comment')->where($where)->avg('comment_level');
            $avg_comment_level = round($avg_comment_level);

            //补全星星等级
            $completion_level = 5 - $avg_comment_level;

            //菜品评价用户数占比
            $store_user_count = M('user')->where(['store_id' => $this->store_id])->count();//当前店铺注册用户总数
//            echo '店铺总用户数：'.$store_user_count;
            $comment_proportion = round(($total / $store_user_count) * 100);//当前菜品评论人数占比;

            //取评价当前菜品的用户头像
            $comment_customer_id = M('comment')->field('customer_id')->where($where)->group('customer_id')->select();
            foreach ($comment_customer_id as $k => $v) {
                $comment_user_headimgurl[$k]['headimgurl'] = M('user')->where(['customer_id' => $v['customer_id'], 'store_id' => $this->store_id])->getField('headimgurl');
            }

//            dump($comment_user_headimgurl);

            $count_data['month_sale'] = $month_sale;//月销
            $count_data['haoping_rate'] = $haoping_rate;//好评率
            $count_data['discount'] = $discount;//折扣
            $count_data['avg_comment_level'] = $avg_comment_level;//平均评论等级
            $count_data['completion_level'] = $completion_level;//补全等级
            $count_data['comment_proportion'] = $comment_proportion;//评论用户数百分比
            $count_data['total'] = $total;//评论总数

            //获取当前菜品用户评论列表
            $comment_list = M('comment')->field('id,username,comment_level,content,image,is_anonymous')->where($where)->where(['examine' => 1])->select();
            foreach ($comment_list as $k => $v) {
                $comment_list[$k]['completion_level'] = 5 - $v['comment_level'];
                if ($comment_list[$k]['is_anonymous'] == 1) {
                    $comment_list[$k]['username'] = '匿名';
                }
            }

        }

        $this->assign("dishes_data", $dishes_data);
        $this->assign("count_data", $count_data);
        $this->assign("comment_user_headimgurl", $comment_user_headimgurl);
        $this->assign("comment_list", $comment_list);
        $this->assign("dishes_attributes_info", $dishes_attributes_info);
        $this->display();
    }

}